﻿package classes
{
	public class Easing
	{

	public static const LINEAR:uint = 0;
	
	public static const BACK_EASE_IN:int = 1;
	public static const BACK_EASE_OUT:int = 2;
	public static const BACK_EASE_IN_OUT:int = 3;
	
	public static const BOUNCE_EASE_IN:int = 4;
	public static const BOUNCE_EASE_OUT:int = 5;
	public static const BOUNCE_EASE_IN_OUT:int = 6;
	
	public static const CIRC_EASE_IN:int = 7;
	public static const CIRC_EASE_OUT:int = 8;
	public static const CIRC_EASE_IN_OUT:int = 9;
	
	public static const CUBIC_EASE_IN:int = 10;
	public static const CUBIC_EASE_OUT:int = 11;
	public static const CUBIC_EASE_IN_OUT:int = 12;
	
	//public static const ELASTIC_EASE_IN:int = 13;
	//public static const ELASTIC_EASE_OUT:int = 14;
	//public static const ELASTIC_EASE_IN_OUT:int = 15;
	
	public static const EXPO_EASE_IN:int = 16;
	public static const EXPO_EASE_OUT:int = 17;
	public static const EXPO_EASE_IN_OUT:int = 18;
	
	public static const QUAD_EASE_IN:int = 19;
	public static const QUAD_EASE_OUT:int = 20;
	public static const QUAD_EASE_IN_OUT:int = 21;
	
	public static const QUART_EASE_IN:int = 22;
	public static const QUART_EASE_OUT:int = 23;
	public static const QUART_EASE_IN_OUT:int = 24;
	
	public static const QUINT_EASE_IN:int = 25;
	public static const QUINT_EASE_OUT:int = 26;
	public static const QUINT_EASE_IN_OUT:int = 27;
	
	public static const SINE_EASE_IN:int = 28;
	public static const SINE_EASE_OUT:int = 29;
	public static const SINE_EASE_IN_OUT:int = 30;
	
	public static function ease(type:int,t:Number,b:Number,c:Number,d:Number,s:Number = 1.70158):Number {
	        	
	        	var retval:Number = 0;
	        	switch(type) {
	        		case 0:
	        			// Linear
	        			return c*t/d + b;
	        		break;
	        		case 1:
	        			// Back Eease In
						return c*(t/=d)*t*((s+1)*t - s) + b;
	        		break;
	        		case 2:
	        			// Back Eease Out
	        			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	        		break;
	        		case 3:
	        			//Back Eease In Out
		        		if ((t/=d/2) < 1) {
		        			return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		        		} else {
							return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
		        		}
	        		break;
	        		case 4:
	        			// Bounce Eease Out
	        			if ((t/=d) < (1/2.75)) {
							return c*(7.5625*t*t) + b;
						} else if (t < (2/2.75)) {
							return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
						} else if (t < (2.5/2.75)) {
							return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
						} else {
							return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
						}
	        		break;
	        		case 5:
	        			// Bounce Eease In
	        			return c - ease(4,d-t, 0, c, d) + b;
	        		break;
	        		case 6:
	        			// Bounce Ease In Out
	        			if (t < d/2) {
	        				return ease (4,t*2, 0, c, d) * .5 + b;
	        			} else {
	        				return ease (4,t*2-d, 0, c, d) * .5 + c*.5 + b;	
	        			}
	        		break;
	        		case 7:
	        			// Circ Ease In
	        			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	        		break;
	        		case 8:
	        			// Circ Ease Out
	        			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	        		break;
	        		case 9:
	        			// Circ Ease In Out
		        		if ((t/=d/2) < 1) { 
		        			return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; 
		        		} else {
							return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
						}
	        		break;
	        		case 10:
	        			// Cubic Ease In
	        			return c*(t/=d)*t*t + b;
	        		break;
	        		case 11:
	        		    // Cubic Ease Out
	        			return c*((t=t/d-1)*t*t + 1) + b;
	        		break;
	        		case 12:
	        			// Cubic Ease In Out
	        			if ((t/=d/2) < 1) { 
	        				return c/2*t*t*t + b;
	        			} else {
							return c/2*((t-=2)*t*t + 2) + b;
						}
	        		break;
	        		case 13:
	 		       	// Elastic Ease In
					// NOT IMPLEMENTED, SORRY
	 		       	return 0;
	        		break;
	        		case 14:
	        			// Elastic Ease Out
					// NOT IMPLEMENTED, SORRY
	        			return 0;
	        		break;
	        		case 15:
	        			// Elastic Ease In Out
					// NOT IMPLEMENTED, SORRY
	        			return 0;
	        		break;
	        		case 16:
	        			// Expo Ease In
	        			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	        		break;
	        		case 17:
	        			// Expo Ease Out
	        			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	        		break;
	        		case 18:
	        			// Expo Ease In Out
	        			if (t==0) return b;
						if (t==d) return b+c;
						if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
						return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	        		break;
	        		case 19:
	        			// Quad Ease In
	        			return c*(t/=d)*t + b;
	        		break;
	        		case 20:
	        			// Quad Ease Out
	        			return -c *(t/=d)*(t-2) + b;
	        		break;
	        		case 21:
	        			// Quad Ease In Out
	        			if ((t/=d/2) < 1) return c/2*t*t + b;
						return -c/2 * ((--t)*(t-2) - 1) + b;
	        		break;
	        		case 22:
	        			// Quart Ease In 
	        			return c*(t/=d)*t*t*t + b;
	        		break;
	        		case 23:
	        			// Quart Ease Out
	        			return -c * ((t=t/d-1)*t*t*t - 1) + b;
	        		break;
	        		case 24:
	        			// Quart Ease In Out
	        			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
						return -c/2 * ((t-=2)*t*t*t - 2) + b;
	        		break;
	        		case 25:
	        			// Quint Ease In
	        			return c*(t/=d)*t*t*t*t + b;
	        		break;
	        		case 26:
	        			// Quint Ease Out
	        			return c*((t=t/d-1)*t*t*t*t + 1) + b;
	        		break;
	        		case 27:
	        			// Quint Ease In Out
	        			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
						return c/2*((t-=2)*t*t*t*t + 2) + b;
	        		break;
	        		case 28:
	        			// Sine Ease In
	        			return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	        		break;
	        		case 29:
	        			// Sine Ease Out
	        			return c * Math.sin(t/d * (Math.PI/2)) + b;
	        		break;
	        		case 30:
	        			// Sine Ease In Out
	        			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	        		break;
	        	}
	        	return retval;
	        }
	}
}